// Package requests 处理请求数据和表单验证
package requests

import (
	"github.com/gofiber/fiber/v2"
	"github.com/thedevsaddam/govalidator"
)

type TripRequest struct {
	VehicleId      uint64  `gorm:"column:vehicle_id;comment:车辆id" db:"vehicle_id" json:"vehicle_id" valid:"vehicle_id"` //车辆ID
	StartLocation  uint64  `gorm:"column:start_location;comment:起始地址id;" db:"start_location" json:"start_location" valid:"start_location"`
	EndLocation    uint64  `gorm:"column:end_location;comment:终点地址id;" db:"end_location" json:"end_location" valid:"end_location"`
	DepartureTime  string  `gorm:"column:departure_time;type:timestamp;comment:出发时间;" db:"departure_time" json:"departure_time" valid:"departure_time"` //出发时间
	Deadline       string  `gorm:"column:deadline;type:timestamp;comment:拼车截止时间;" db:"deadline" json:"deadline" valid:"deadline"`                       //拼车截止时间
	AvailableSeats uint8   `gorm:"column:available_seats;comment:剩余座位数;" db:"available_seats" json:"available_seats" valid:"available_seats"`           //可用座位数
	Price          float64 `gorm:"column:price;comment:单价;" db:"price" json:"price" valid:"price"`                                                      //每个座位的价格
	Itinerary      string  `gorm:"type:varchar(255);comment:行程安排;" db:"itinerary" json:"itinerary" valid:"itinerary"`                                   //行程安排
}

// https://pkg.go.dev/github.com/thedevsaddam/govalidator@v1.9.10#section-readme
func ValidateTrip(c *fiber.Ctx, request *TripRequest) map[string][]string {

	c.BodyParser(request)
	rules := govalidator.MapData{
		"vehicle_id":      []string{"required"},
		"start_location":  []string{"required"},
		"end_location":    []string{"required"},
		"departure_time":  []string{"required", "datetime"},
		"deadline":        []string{"required", "datetime"},
		"available_seats": []string{"required", "numeric", "min:1", "max:7"},
		"price":           []string{"required", "between:0,1000000"},
		"itinerary":       []string{"max_cn:255"},
	}

	messages := govalidator.MapData{
		// "name": []string{
		//     "required:名称为必填项",
		//     "min_cn:名称长度需至少 2 个字",
		//     "max_cn:名称长度不能超过 8 个字",
		// },
		// "description": []string{
		//     "min_cn:描述长度需至少 3 个字",
		//     "max_cn:描述长度不能超过 255 个字",
		// },
	}

	return Validate(request, rules, messages)
}
